package fast.mvc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import fast.loader.Gem;
import fast.loader.Item;
import fast.loader.ItemEquipped;
import fast.loader.Socket;
import fast.ranking.EP;


public class ItemPanel extends JPanel {
	
	private static final long serialVersionUID = 852930853916977746L;
	private int selected = 0;
	HashMap<JButton,Item> items = new HashMap<JButton,Item>();
	
	public ItemPanel(){
		this.setLayout(null);
	}

	public HashMap<JButton,Item> getItems(){
		return items;
	}
	
	public void addItemChangeListener(JButton i,ActionListener l){
        i.addActionListener(l);
    }

	public void showItems(Iterator<EP<Item>> it, ItemEquipped curItem) {
		int count = 0;
		double max = 0;
		while (it.hasNext()) {
			Item item = it.next().getItem();
			double ep = 0.0;
			if(item != null)
				ep = item.getEp();
			if(max == 0) max = ep;
			boolean equiped = false;
			if (item == curItem.getItem())
				equiped = true;
			
			String name = "None";
			String icon = "inv_misc_questionmark";
			if(item != null){
				name = item.getName();
				icon = item.getIcon();
			}
			JPanel itemContainer = new JPanel(null);
			itemContainer.setBounds(0, count*55, 320, 55);
			if(equiped){
				itemContainer.setBackground(Color.decode("#D1D0D0"));
				selected = count;
			}else
				itemContainer.setBackground(null);
			this.add(itemContainer);
			
			JButton btnItem = new JButton();
			if(equiped)
				btnItem.setBorder(new LineBorder(Color.GREEN,3));
			else
				btnItem.setBorder(new LineBorder(Color.BLACK,2));
			btnItem.setBounds(0, 0, 54, 54);
			if(item != null)
				btnItem.setToolTipText("<html>"+item.getTooltip()+"</html>");

			ChangeIconTask task = new ChangeIconTask(btnItem,icon);
			task.execute();

			items.put(btnItem,item);
			itemContainer.add(btnItem);
			
			String tag = "";
			if(!item.getTag().equals(""))
				tag = item.getTag()+", ";
			
			JLabel labelName = new JLabel(name + " (" + tag + item.getItemLevel() + ")");
			if(item.getQuality() == 3)
				labelName.setForeground(Color.decode("#0080ff"));
			if(item.getQuality() == 4)
				labelName.setForeground(Color.decode("#b048f8"));
			if(item.getQuality() == 5)
				labelName.setForeground(Color.decode("#ff8000"));
			labelName.setBounds(55, 0, 250, 20);
			labelName.setFont(new Font("sansserif",Font.PLAIN,10));
			itemContainer.add(labelName);
			
			String speed = "";
			if(item.getSpeed() != 0.0){
				speed += item.getSpeed();
			}
			String gearBuff = "";
			if(item.getGearBuff() != null){
				gearBuff = item.getGearBuff();
			}
			String set = "";
			if(item.getSet() != null){
				set = item.getSet();
			}
			String wtype = "";
			if(item.getWeaponType() != null){
				wtype = item.getWeaponType().getName();
			}
			String info = set + " " + wtype + " " + speed + " " + gearBuff + " ";
			info = info.trim() + " ";
			info += item.getStatString();
			info = " " + info.trim();
			JLabel labelInfo = new JLabel(info);
			labelInfo.setBounds(55, 20, 220, 20);
			labelInfo.setFont(new Font("sansserif",Font.PLAIN,10));
			JLabel infoContainer = new JLabel();
			infoContainer.setBounds(55, 20, 220, 20);
			infoContainer.setLayout(new BoxLayout(infoContainer, BoxLayout.X_AXIS));
			for(Socket slotColor : item.getGemSlot()){
				if(slotColor != null){
					JLabel iconLabel = new JLabel();
					ChangeIconTask task2 = new ChangeIconTask(iconLabel,"UI-EmptySocket-"+slotColor,false,true);
					task2.execute();
					infoContainer.add(iconLabel);
				}
			}
			infoContainer.add(labelInfo);
			itemContainer.add(infoContainer);
			
			ItemEquipped sug = item.getSuggestion().getItemEquipped();
			String infoS = "";
			if(sug != null){
				for(Gem gem : sug.gem){
					if(gem != null)
						infoS += gem + " ";
				}
			}
			if(sug != null && sug.getReforge() != null)
				infoS += sug.getReforge().getFrom() + " -> " + sug.getReforge().getTo();
			JLabel labelSuggestion = new JLabel(infoS);
			labelSuggestion.setBounds(60, 37, 220, 20);
			labelSuggestion.setFont(new Font("sansserif",Font.PLAIN,10));
			itemContainer.add(labelSuggestion);
			
			double diff = ep;
        	//if(!equiped)
        	//	diff -= curItem.getEp();
        	
            JLabel labelEp = new JLabel(String.format("%.2f", diff));
            labelEp.setBounds(155, 20,160,20);
            labelEp.setHorizontalAlignment(JLabel.RIGHT);
			itemContainer.add(labelEp);
			
			RatingPanel rp = new RatingPanel(ep, max);
			rp.setLocation(55, 40);
			itemContainer.add(rp);
			
			if(item != null){
				JLabel ilevel = new JLabel(item.getItemLevel()+"");
				ilevel.setBounds(0,0,20,20);
				itemContainer.add(ilevel);
			}
			count++;
		}
		this.setPreferredSize(new Dimension(300,1+count*55));
		this.setSize(new Dimension(300,1+count*55));
	}

	public int getSelectedPosition() {
		if(selected >= 3)
			selected -= 3;
		else
			selected = 0;
		return selected * 55;
	}
}
